package com.zhongxi.guoyuan.view.personalCenter;

import android.view.View;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.MessageAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.user.center.MessageData;
import com.zhongxi.guoyuan.entity.user.center.MessageResult;
import com.zhongxi.guoyuan.url.Url;

import java.util.ArrayList;
import java.util.List;

public class SystemMessageActivity extends BaseActivity implements View.OnClickListener {

    private PullToRefreshListView lvList;
    private MessageAdapter adapter;
    private List<MessageData> data = new ArrayList<>();

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_dynamics);
        initTitle();
        tvTitle.setText("系统通知");
        lvList = (PullToRefreshListView) findViewById(R.id.lvList);
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new MessageAdapter(context, data);
        lvList.setAdapter(adapter);
        getMessage();
    }

    private void getMessage() {
        getServer(Url.SYS_NOTIFY, null, "", false, null, "get");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            MessageResult result = gson.fromJson(response, MessageResult.class);
            if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj()))
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            data.addAll(result.getData());
            adapter.notifyDataSetChanged();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try{
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
        }
    }
}
